package cn.chenxing.module.query.controller;

import cn.chenxing.domain.dto.TrackData;
import cn.chenxing.domain.dto.TrackLine;
import cn.chenxing.domain.dto.TrackPoint;
import cn.chenxing.domain.dto.TrackTable;
import cn.chenxing.domain.threadlocal.ThreadData;
import cn.chenxing.domain.util.DialogBuilderUtil;
import cn.chenxing.utils.TableUtil;
import cn.chenxing.domain.util.DateUtil;
import com.jfoenix.controls.JFXDatePicker;
import com.jfoenix.controls.JFXTimePicker;
import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.layout.BorderPane;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;

import java.net.URL;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @Description 时间查询Controller层
 * @Author maogen.ymg
 * @Date 2020/4/13 22:51
 */
@Lazy
@FXMLController
@Slf4j
public class QueryTimeController implements Initializable {

    @FXML
    private JFXDatePicker dateStart;

    @FXML
    private JFXTimePicker timeStart;

    @FXML
    private JFXDatePicker dateEnd;

    @FXML
    private JFXTimePicker timeEnd;

    @FXML
    private BorderPane borderPane;

    @FXML
    private Label tipLabel;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        timeStart.set24HourView(true);
        timeEnd.set24HourView(true);
    }

    @FXML
    public void query() {
        if(ThreadData.get().getTrackData() == null) {
            DialogBuilderUtil.showPositiveAlert(dateStart, "无法进行查询", "还没导入数据，请选择文件菜单进行导入");
            return;
        }

        long timeStampStart;
        long timeStampEnd;
        try {
            // 开始时间戳
            LocalDateTime localDateTimeStart = LocalDateTime.of(dateStart.getValue(), timeStart.getValue());
            timeStampStart = DateUtil.localDateTime2TimeStamp(localDateTimeStart);
            // 结束时间戳
            LocalDateTime localDateTimeEnd = LocalDateTime.of(dateEnd.getValue(), timeEnd.getValue());
            timeStampEnd = DateUtil.localDateTime2TimeStamp(localDateTimeEnd);
        }catch (Exception e) {
            DialogBuilderUtil.showPositiveAlert(dateStart, "提示", "请完整填写日期时间");
            return;
        }

        DialogBuilderUtil.showPositiveAlert(timeStampStart > timeStampEnd,
                dateStart, "请注意", "填写的日期时间有误，请重新输入！");

        // 显示在表格上
        TableUtil.importTableView(borderPane, getTableData(timeStampStart, timeStampEnd), false);

    }

    @FXML
    public void clear() {
        dateStart.setValue(null);
        dateEnd.setValue(null);
        timeStart.setValue(null);
        timeEnd.setValue(null);
        borderPane.getChildren().clear();
        borderPane.setCenter(tipLabel);
    }

    /**
     * 获取表格数据
     * @return List<TrackTable>
     */
    private List<TrackTable> getTableData(long timeStampStart, long timeStampEnd) {

        List<TrackTable> trackTableList = new ArrayList<>();
        // 用户数据
        List<TrackLine> trackLineList = ThreadData.get().getTrackData().getTrackLineList();
        for(TrackLine trackLine : trackLineList) {
            long userId = trackLine.getId();
            for(TrackPoint trackPoint : trackLine.getTrackPointList()) {
                // 判断时间戳
                if(trackPoint.getTimeStamp() >= timeStampStart && trackPoint.getTimeStamp() <=timeStampEnd) {
                    TrackTable trackTable = new TrackTable();
                    trackTable.setId(userId)
                            .setLongitude(trackPoint.getLongitude())
                            .setLatitude(trackPoint.getLatitude())
                            .setTime(DateUtil.timeStamp2Date(trackPoint.getTimeStamp(), null))
                            .setSpeed(trackPoint.getSpeed());
                    trackTableList.add(trackTable);
                }
            }

        }

        // 保存为公有数据
        TrackData trackData = ThreadData.get().getTrackData();
        trackData.setTrackLineList(trackLineList);
        ThreadData.get().setTrackData(trackData);

        return trackTableList;
    }

}
